package com.tuiyun.jxgl.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.jxgl.dao.StudentHome;
import com.tuiyun.util.PWDUtil;

public class EditPwdAction {
	
	private Student student;
	private Driver driver;
	private String msg;
	
	public String fill(){
		msg = null;		
		return "success";
	}
	@Transactional
	public String execute(){
		if(id != -1){
			return "editother";
		}else{
			//student or driver edit itself
			Subject subject = SecurityUtils.getSubject();		
	        if (subject != null) {
	        	Object o = subject.getPrincipal();
	        	if(o instanceof Student){
		            Student s = (Student) o;
		            s.setPwd(PWDUtil.encryptPWD(getPwd()));
		            sh.attachDirty(s);
		            setStudent(s);		           
	            }else{
	            	Driver d = (Driver)o;
	            	d.setPwd(PWDUtil.encryptPWD(getPwd()));
	            	dh.attachDirty(d);
	            	setDriver(d);
	            }
	        }
		}
		msg = "编辑密码成功！";
		return "success";
	}
	
	private StudentHome sh;
	private DriverHome dh;
	private int id = -1;
	private String pwd;

	public StudentHome getSh() {
		return sh;
	}
	public void setSh(StudentHome sh) {
		this.sh = sh;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public DriverHome getDh() {
		return dh;
	}
	public void setDh(DriverHome dh) {
		this.dh = dh;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setDriver(Driver driver) {
		this.driver = driver;
	}
}
